package com.qch.rocketmq.springboot_ms_rocketmq.service;

import com.alibaba.fastjson.JSON;
import com.qch.rocketmq.springboot_ms_rocketmq.cache.RedisService;
import com.qch.rocketmq.springboot_ms_rocketmq.pojo.GoodDO;
import com.qch.rocketmq.springboot_ms_rocketmq.pojo.OrderDO;
import com.qch.rocketmq.springboot_ms_rocketmq.repositry.GoodsRepository;
import com.qch.rocketmq.springboot_ms_rocketmq.rocketmq.consumer.MyProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * @Author qch
 * @Date 2021/6/23
 */
@Slf4j
@Service
public class GoodsService {
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MyProducer myProducer;
    public List<GoodDO> getAllList() {
        List<GoodDO> list  = goodsRepository.findAll();
        if (!list.isEmpty()) {
            list.forEach(item -> {
                redisService.setValue("goods:" + item.getId(), item, 60);
            });
        }
        return list;
    }
    public GoodDO seckill(Long goodsId, String userId,String orderId) {
        GoodDO one = (GoodDO) redisService.getValue("goods:" + goodsId, GoodDO.class);
        if(one==null){
             one = goodsRepository.getOne(goodsId);
        }
        if (one != null && one.getReserveNum() > 0) {
            OrderDO orders = new OrderDO();
            orders.setGoodId(goodsId);
            orders.setUserId(userId);
            orders.setOrderNo(orderId);
            myProducer.sendMessage(JSON.toJSONString(orders));
        }
        return one;
    }

    @Transactional
    public synchronized void proOrder(OrderDO orderDO) {
        try {
            log.info("orders: {}", orderDO);
            if (orderDO != null) {
                // 先查询一次缓存
                GoodDO cache = (GoodDO) redisService.getValue("goods:" + orderDO.getGoodId(), GoodDO.class);
                if (cache != null
                        && cache.getReserveNum() == 0) {
                    return;
                }

                Optional<GoodDO> optional = goodsRepository.findById(orderDO.getGoodId());
                GoodDO goods = optional.isPresent() ? optional.get() : null;
                if (goods != null && goods.getReserveNum() > 0) {
                    OrderDO orderServiceByUserIdAndGoodId = orderService.findByOrderId(orderDO.getOrderNo());
                    if (orderServiceByUserIdAndGoodId != null)
                        return;

                    // 库存-1
                    goods.setReserveNum(Math.max(goods.getReserveNum() - 1, 0));
                    goodsRepository.save(goods);

                    orderDO.setStatus(0);
                    orderService.save(orderDO);
                    // 刷新缓存
                    redisService.setValue("goods:"+goods.getId(), goods, 60);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
